package com.lzxuni.modules.dataAcquisition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.dataAcquisition.entity.ChainLink;
import com.lzxuni.modules.dataAcquisition.entity.ChainLinkProcess;
import com.lzxuni.modules.dataAcquisition.mapper.ChainLinkProcessMapper;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 追溯链条-环节-过程 服务实现类
 * </p>
 *
 * @author wzx
 * @since 2019-06-25
 */
@Service
public class ChainLinkProcessServiceImpl extends ServiceImpl<ChainLinkProcessMapper, ChainLinkProcess> implements ChainLinkProcessService {

    @Autowired
    private ChainLinkProcessMapper chainLinkProcessMapper;
    @Autowired
    private ChainLinkService chainLinkService;

    @Override
    public PageInfo<ChainLinkProcess> queryPage(PageParameter pageParameter, ChainLinkProcess chainLinkProcess) throws SQLException {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<ChainLinkProcess> list;
        if (StringUtils.isNotEmpty(chainLinkProcess.getProcessName())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLinkProcess>().like("process_name", chainLinkProcess.getProcessName()));
        }else {
            list = baseMapper.selectList(new QueryWrapper<>());
        }
        if (list != null && list.size() > 0) {
            for (ChainLinkProcess chainLinkProcess1 : list) {
                ChainLink chainLink = chainLinkService.getById(chainLinkProcess1.getLinkId());
                chainLinkProcess1.setLinkName(chainLink == null ? null : chainLink.getLinkName());
            }
        }
        PageInfo<ChainLinkProcess> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Tree> getTreeSelectTree(String nature) {
        List<Tree> list = new ArrayList<>();
        List<ChainLinkProcess> chainLinkProcesses = baseMapper.selectList(new QueryWrapper<>());
        if (chainLinkProcesses != null && chainLinkProcesses.size() > 0) {
            for (ChainLinkProcess chainLinkProcess : chainLinkProcesses) {
                Tree tree = new Tree();
                tree.setId(chainLinkProcess.getId());
                tree.setParentId("");
                tree.setText(chainLinkProcess.getProcessName());
                tree.setTitle(chainLinkProcess.getProcessName());
                tree.setShowcheck(false);
                tree.setCheckstate(0);
                tree.setComplete(true);
                list.add(tree);
            }
        }
        return list;
    }
}
